package org.example.ding.config;

import org.example.ding.entity.User;
import org.example.ding.entity.DishCategory;
import org.example.ding.entity.Dish;
import org.example.ding.repository.UserRepository;
import org.example.ding.repository.DishCategoryRepository;
import org.example.ding.repository.DishRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;

/**
 * 数据初始化器
 * 在应用启动时创建默认数据
 * 暂时禁用，使用DataInitializationService代替
 */
//@Component
public class DataInitializer implements CommandLineRunner {

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private DishCategoryRepository dishCategoryRepository;

    @Autowired
    private DishRepository dishRepository;

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Override
    public void run(String... args) throws Exception {
        initializeUsers();
        initializeCategories();
        initializeDishes();
    }

    /**
     * 初始化用户数据
     */
    private void initializeUsers() {
        // 检查是否已存在admin用户
        if (userRepository.findByUsername("admin").isEmpty()) {
            User admin = new User();
            admin.setUsername("admin");
            admin.setPassword(passwordEncoder.encode("admin123"));
            admin.setEmail("admin@nutrition.com");
            admin.setRealName("系统管理员");
            admin.setGender(User.Gender.OTHER);
            admin.setAge(30);
            admin.setHeight(new BigDecimal("170.00"));
            admin.setWeight(new BigDecimal("65.00"));
            admin.setActivityLevel(User.ActivityLevel.MODERATE);
            
            // 设置营养目标
            admin.setDailyCaloriesGoal(new BigDecimal("2000"));
            admin.setDailyProteinGoal(new BigDecimal("150"));
            admin.setDailyCarbsGoal(new BigDecimal("250"));
            admin.setDailyFatGoal(new BigDecimal("65"));
            
            admin.setIsActive(true);
            
            userRepository.save(admin);
            System.out.println("✅ 创建默认管理员用户: admin/admin123");
        }

        // 创建测试用户
        if (userRepository.findByUsername("testuser").isEmpty()) {
            User testUser = new User();
            testUser.setUsername("testuser");
            testUser.setPassword(passwordEncoder.encode("123456"));
            testUser.setEmail("test@nutrition.com");
            testUser.setRealName("测试用户");
            testUser.setGender(User.Gender.MALE);
            testUser.setAge(25);
            testUser.setHeight(new BigDecimal("175.00"));
            testUser.setWeight(new BigDecimal("70.00"));
            testUser.setActivityLevel(User.ActivityLevel.MODERATE);
            
            // 设置营养目标
            testUser.setDailyCaloriesGoal(new BigDecimal("2200"));
            testUser.setDailyProteinGoal(new BigDecimal("165"));
            testUser.setDailyCarbsGoal(new BigDecimal("275"));
            testUser.setDailyFatGoal(new BigDecimal("70"));
            
            testUser.setIsActive(true);
            
            userRepository.save(testUser);
            System.out.println("✅ 创建测试用户: testuser/123456");
        }
    }

    /**
     * 初始化菜品分类
     */
    private void initializeCategories() {
        if (dishCategoryRepository.count() == 0) {
            // 早餐分类
            createCategory("主食类", "米饭、面条、包子等主食", DishCategory.MealType.BREAKFAST);
            createCategory("蛋类", "鸡蛋、鸭蛋等蛋类制品", DishCategory.MealType.BREAKFAST);
            createCategory("奶制品", "牛奶、酸奶、豆浆等", DishCategory.MealType.BREAKFAST);
            
            // 午餐分类
            createCategory("荤菜", "猪肉、牛肉、鸡肉、鱼类等", DishCategory.MealType.LUNCH);
            createCategory("素菜", "各种蔬菜类菜品", DishCategory.MealType.LUNCH);
            createCategory("汤类", "各种汤品", DishCategory.MealType.LUNCH);
            
            // 晚餐分类
            createCategory("清淡菜", "适合晚餐的清淡菜品", DishCategory.MealType.DINNER);
            createCategory("粥类", "各种粥品", DishCategory.MealType.DINNER);
            
            // 小食分类
            createCategory("水果", "各种新鲜水果", DishCategory.MealType.SNACK);
            createCategory("坚果", "各种坚果类零食", DishCategory.MealType.SNACK);
            
            System.out.println("✅ 创建默认菜品分类");
        }
    }

    /**
     * 初始化菜品数据
     */
    private void initializeDishes() {
        if (dishRepository.count() == 0) {
            // 获取分类
            DishCategory breakfast = dishCategoryRepository.findByNameAndMealType("主食类", DishCategory.MealType.BREAKFAST).orElse(null);
            DishCategory lunch = dishCategoryRepository.findByNameAndMealType("荤菜", DishCategory.MealType.LUNCH).orElse(null);
            DishCategory dinner = dishCategoryRepository.findByNameAndMealType("清淡菜", DishCategory.MealType.DINNER).orElse(null);
            DishCategory snack = dishCategoryRepository.findByNameAndMealType("水果", DishCategory.MealType.SNACK).orElse(null);

            // 创建示例菜品
            if (breakfast != null) {
                createDish("白米粥", "营养丰富的白米粥", breakfast, 
                    new BigDecimal("46"), new BigDecimal("1.0"), new BigDecimal("10.0"), new BigDecimal("0.1"),
                    new BigDecimal("5.00"), Dish.SpiceLevel.NONE, Dish.CookingMethod.BOILED);
                
                createDish("煮鸡蛋", "营养丰富的水煮鸡蛋", breakfast,
                    new BigDecimal("155"), new BigDecimal("13.0"), new BigDecimal("1.1"), new BigDecimal("11.0"),
                    new BigDecimal("3.00"), Dish.SpiceLevel.NONE, Dish.CookingMethod.BOILED);
            }

            if (lunch != null) {
                createDish("红烧肉", "经典红烧肉", lunch,
                    new BigDecimal("395"), new BigDecimal("14.0"), new BigDecimal("3.8"), new BigDecimal("37.0"),
                    new BigDecimal("25.00"), Dish.SpiceLevel.MILD, Dish.CookingMethod.FRIED);
                
                createDish("清蒸鱼", "新鲜清蒸鱼", lunch,
                    new BigDecimal("112"), new BigDecimal("22.0"), new BigDecimal("0.0"), new BigDecimal("2.0"),
                    new BigDecimal("18.00"), Dish.SpiceLevel.NONE, Dish.CookingMethod.STEAMED);
            }

            if (dinner != null) {
                createDish("蔬菜沙拉", "新鲜蔬菜沙拉", dinner,
                    new BigDecimal("65"), new BigDecimal("2.0"), new BigDecimal("8.0"), new BigDecimal("3.0"),
                    new BigDecimal("12.00"), Dish.SpiceLevel.NONE, Dish.CookingMethod.RAW);
            }

            if (snack != null) {
                createDish("苹果", "新鲜苹果", snack,
                    new BigDecimal("52"), new BigDecimal("0.3"), new BigDecimal("14.0"), new BigDecimal("0.2"),
                    new BigDecimal("3.00"), Dish.SpiceLevel.NONE, Dish.CookingMethod.RAW);
            }

            System.out.println("✅ 创建示例菜品数据");
        }
    }

    private void createCategory(String name, String description, DishCategory.MealType mealType) {
        DishCategory category = new DishCategory();
        category.setName(name);
        category.setDescription(description);
        category.setMealType(mealType);
        dishCategoryRepository.save(category);
    }

    private void createDish(String name, String description, DishCategory category,
                           BigDecimal calories, BigDecimal protein, BigDecimal carbs, BigDecimal fat,
                           BigDecimal price, Dish.SpiceLevel spiceLevel, Dish.CookingMethod cookingMethod) {
        Dish dish = new Dish();
        dish.setName(name);
        dish.setDescription(description);
        dish.setCategory(category);
        dish.setCalories(calories);
        dish.setProtein(protein);
        dish.setCarbohydrates(carbs);
        dish.setFat(fat);
        dish.setPrice(price);
        dish.setSpiceLevel(spiceLevel);
        dish.setCookingMethod(cookingMethod);
        dish.setIsAvailable(true);
        dish.setOrderCount(0);
        dish.setPopularityScore(new BigDecimal("50"));
        dishRepository.save(dish);
    }
}
